<template>
  <div>
    <!-- 头像资料区域 -->
    <div class="profile-box">
      <!-- 左边头像 -->
      <!-- 我们装的把px转成vw的插件，只支持css样式，不支持行内样式 -->
      <van-image class="avatar" round fit="cover" :src="userInfo.photo" />
      <!-- 右边文字 -->
      <div class="text-box">
        <h3 class="name">{{ userInfo.name }}</h3>
        <span class="birthday">{{ userInfo.birthday }}</span>
      </div>
    </div>
    <!-- 快捷入口区域 -->
    <van-grid :border="false" :column-num="3">
      <van-grid-item
        style="color: #3296fa"
        icon="newspaper-o"
        text="我的作品"
      />
      <van-grid-item style="color: #f00" icon="star-o" text="我的收藏" />
      <van-grid-item style="color: orange" icon="tosend" text="阅读历史" />
    </van-grid>
    <!-- 功能cell -->
    <van-cell-group>
      <van-cell to="/mine/edit" title="编辑资料" icon="edit" is-link />
      <van-cell to="/chat" title="小智同学" icon="chat-o" is-link />
      <van-cell title="系统设置" icon="setting-o" is-link />
      <van-cell @click="logout" title="退出登录" icon="info-o" is-link />
    </van-cell-group>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { Dialog } from 'vant'
export default {
  name: 'MyMine',
  computed: {
    ...mapState('user', ['userInfo'])
  },
  created () {
    this.$store.dispatch('user/reqUserInfo')
  },

  methods: {
    logout () {
      Dialog.confirm({
        title: '退出',
        message: '确定要退出吗？'
      })
        .then(() => {
          // 删除token
          this.$store.commit('user/setToken', {})
          this.$store.commit('user/setUserInfo', {})
          this.$toast('退出成功！')
          this.$router.push('/login')
        })
        .catch(() => {
          console.log('取消了')
        })
    }
  }
}
</script>

<style lang="less" scoped>
.profile-box {
  @bg: #3296fa;
  height: 140px;
  background-color: @bg;
  display: flex;
  align-items: center;

  .avatar {
    width: 120px;
    height: 120px;
    margin-left: 20px;
    margin-right: 10px;
  }

  .text-box {
    .name {
      color: #fff;
      font-size: 20px;
      margin-bottom: 10px;
    }
    .birthday {
      background-color: #fff;
      color: @bg;
      padding: 3px 2px;
    }
  }
}
</style>
